package com.wondersgroup.insurance.dnuilearn.views.svg_map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.wondersgroup.insurance.dnuilearn.R;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by Administrator on 2017/6/1.
 */

public class TaiWanView extends View {
    Paint strokePaint, fillPaint;


    public LinkedList<ItemPath> list;
    ItemPath selectItem;

    public TaiWanView(Context context) {
        this(context, null);
    }

    public TaiWanView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaiWanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    void init(){
        strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        strokePaint.setColor(Color.GRAY);
        strokePaint.setStrokeWidth(1);
        strokePaint.setStyle(Paint.Style.STROKE);

        fillPaint  = new Paint(Paint.ANTI_ALIAS_FLAG);
        fillPaint.setStyle(Paint.Style.FILL);
        fillPaint.setStrokeWidth(4);
        parserSVG();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (list !=null && list.size() >0){
            for (ItemPath item : list){
                item.draw(canvas,strokePaint,fillPaint);
            }
        }
    }

    void parserSVG(){
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        SAXParserFactory factory = SAXParserFactory.newInstance();
                        SAXParser parser = null;
                        try {
                            parser = factory.newSAXParser();
                            XMLReader reader = parser.getXMLReader();
                            ProvinceSVGHandler handler = new ProvinceSVGHandler();
                            reader.setContentHandler(handler);
                            InputSource is = new InputSource(getResources().openRawResource(R.raw.taiwanhigh));//取得本地xml文件
                            reader.parse(is);
                            list = handler.list;
                            if (list !=null && list.size() >0){
                                Log.e("","list.size() = "+list.size());
                            }
                            postInvalidate();
                        } catch (ParserConfigurationException e) {
                            e.printStackTrace();
                        } catch (SAXException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
        ).start();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("","onTouchEvent");
        return gestureDetectorCompat.onTouchEvent(event);
//        return gestureDetector.onTouchEvent(event);
    }

    GestureDetectorCompat gestureDetectorCompat = new GestureDetectorCompat(getContext(),new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.e("","onSingleTapUp");
//            showToast("onSingleTapUp");
            compute((int)e.getX(),(int)e.getY());

            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            super.onShowPress(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {

            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.e("","onSingleTapConfirmed");
//            showToast("onSingleTapConfirmed");
            return true;
        }

        @Override
        public boolean onContextClick(MotionEvent e) {
            Log.e("","onContextClick");
//            showToast("onContextClick");

            return true;
        }
    });



    public  void showToast(String str){
        Toast.makeText(getContext(),str , Toast.LENGTH_SHORT).show();
    }

    void compute(int x, int y){
        if (selectItem != null){
            selectItem.isSelect = false;
        }
        selectItem = null;
        if (list != null && list.size() >0){
            for (ItemPath item : list){
                if (item.isSelect(x,y)){
                    selectItem = item;
                    selectItem.isSelect = true;
                    break;
                }
            }
        }
        if (selectItem !=null){
            invalidate();
        }
    }

}